package com.seaengine.net
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class Buffer extends Codec
	{
		public function Buffer()
		{
			super();
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */
		public function clearMemory():uint
		{
			for(var i:uint = 0; i < this.length ;++i)
			{
				this[i] = 0;
			}
			return this.length;
		}
		
		/**
		 * 
		 * @param buffer
		 * @return 
		 * 
		 */
		public function cloneBuffer(buffer:Buffer):uint
		{
			this.clearMemory();
			this.seek(0);
			buffer.seek(0);
			this.writeBytes(buffer);
			return buffer.length;
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */
		public function get rawByteArray():ByteArray
		{
			var rawPos:uint = this.position;
			this.seek();
			var res:Buffer = new Buffer();
			res.endian = Endian.LITTLE_ENDIAN;
			for(var i:int = 0; i< this.length;++i)
			{
				res.writeByte(this.decodeInt8(-1));
			}
			res.position = 0;
			this.position = rawPos;
			return res;
		}
		
		/**
		 * 
		 * @param i
		 * @param index
		 * 
		 */
		public function writeByteAt(i:int,index:uint):void
		{
			this[index] = i;
		}
		/**
		 * 
		 * @param index
		 * @return 
		 * 
		 */
		public function readByteAt(index:uint):int
		{
			return this[index];
		}
		/**
		 * 
		 * @param s
		 * @param index
		 * 
		 */
		public function writeShortAt(s:int,index:uint):void
		{
			var ba:Buffer = new Buffer();
			ba.writeShort(s);
			for(var i:int = 0 ;i<ba.length;++i)
			{
				this[index++] = ba[i];
			}
		}
		/**
		 * 
		 * @param ba
		 * @param index
		 * @param offset
		 * @param len
		 * 
		 */
		public function writeBytesAt(ba:ByteArray,index:uint,offset:uint=0,len:uint=0):void
		{
			if(len == 0)
			{
				len = ba.length;
			}
			for(var i:int = 0 ; i<len ; i++)
			{
				this[index++] = ba[offset++];
			}
		}
		/**
		 * 
		 * @param index
		 * @param len
		 * @return 
		 * 
		 */
		public function readBytesAt(index:uint=0,len:uint=0):Buffer
		{
			if((index+len) > this.length)
			{
				return null;
			}
			var res:Buffer = new Buffer();
			if( 0 == len)
			{
				len = this.length - index;
			}
			for(var i:uint = 0 ; i < len ;++i)
			{
				res[i] = this[index++];
			}
			return res;
		}
		/**
		 * 
		 * @param i
		 * @param index
		 * 
		 */
		public function writeIntAt(i:int,index:uint):void
		{
			var ba:Buffer = new Buffer();
			ba.writeInt(i);
			for(var i:int = 0 ;i<ba.length;++i)
			{
				this[index++] = ba[i];
			}
		}
		/**
		 * 
		 * @param u
		 * @param index
		 * 
		 */
		public function writeUIntAt(u:uint,index:uint):void
		{
			var ba:Buffer = new Buffer();
			ba.writeUnsignedInt(u);
			for(var i:int = 0 ;i<ba.length;++i)
			{
				this[index++] = ba[i];
			}
		}
		/**
		 * 
		 * @param value
		 * @param index
		 * @param charSet
		 * 
		 */
		public function writeMultiByteAt(value:String,index:uint,charSet:String):void
		{
			var ba:Buffer = new Buffer();
			ba.writeMultiByte(value,charSet);
			for(var i:int = 0 ;i<ba.length;++i)
			{
				this[index++] = ba[i];
			}
		}
		/**
		 * 
		 * @param index
		 * @param len
		 * @return 
		 * 
		 */
		public function truncateBuffer(index:uint,len:uint):JoyPortBuffer
		{
			if(!len || len < 0)
				return null;
			if(len > this.bytesAvailable)
				len = this.bytesAvailable;
			var res:JoyPortBuffer = new JoyPortBuffer();
			/*if(this.position != index)
			{
				this.seek(index);
			}
			for(var i:uint = 0; i < len; ++i)
			{
				res.encodeInt8(this.readByte());
			}*/
			res.writeBytes(this,index,len);
			this.position += len;
			res.seek();
			return res;	
		}
		
		/**
		 * 
		 * @param buffer
		 * 
		 */
		public function append(buffer:Buffer):void
		{
			/*for(var i:int = 0 ; i < buffer.length ; ++i)
			{
				this.writeByte(buffer.readByte());
			}*/
			buffer.seek();
			this.seek(this.length);
			this.writeBytes(buffer);
			this.seek();
		}
	}
}